package tool

import (
	"context"
	"fmt"
	"github.com/qiniu/api.v7/v7/auth/qbox"
	"github.com/qiniu/api.v7/v7/storage"
	"mime/multipart"
	"time"
)



func UpLoadFile(file multipart.File, fileSize int64) (string, string) {

	var AccessKey = GetConf().QiLiuyunConfig.AccessKey
	var SecretKey = GetConf().QiLiuyunConfig.SecretKey
	var Bucket = GetConf().QiLiuyunConfig.Bucket
	var ImgUrl = GetConf().QiLiuyunConfig.QiniuServer
	putPolicy := storage.PutPolicy{
		Scope: Bucket,
	}
	mac := qbox.NewMac(AccessKey, SecretKey)
	upToken := putPolicy.UploadToken(mac)

	cfg := storage.Config{}
	// 空间对应的机房
	cfg.Zone = &storage.ZoneHuanan
	// 是否使用https域名
	cfg.UseHTTPS = true
	// 上传是否使用CDN上传加速
	cfg.UseCdnDomains = true

	putExtra := storage.PutExtra{}

	formUploader := storage.NewFormUploader(&cfg)
	ret := storage.PutRet{}


	key := fmt.Sprintf("%d", time.Now().UnixNano())
	err := formUploader.Put(context.Background(), &ret, upToken, key,file, fileSize, &putExtra)
	if err != nil {
		fmt.Println("上传失败")
		fmt.Println(err)
		return "", "500"
	}
	url := ImgUrl + ret.Key
	return url, "200"
}